package scau.gsmg.misp.action;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSON;
import net.sf.json.JSONArray;

import org.apache.shiro.SecurityUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import scau.gsmg.misp.service.IMenuService;
import scau.gsmg.misp.utils.ShiroUtil;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @Title: MenuAction.java
 * @Group: GaoShenMenGui
 * @Author: WXY
 * @Date: 2015-5-9
 */
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Results({
		@Result(name = "success", type = "json", params = { "root", "root" }),
		@Result(name = "goodsEdit", location = "/editGoods.jsp") ,
		@Result(name = "jsonArray", type = "json", params = { "root",
		"jsonArray" }) ,
		@Result(name = "jsonObj", type = "json", params = { "root",
		"jsonObj" }) 
		})
@Controller
public class MenuAction extends ActionSupport {

	private int role;
	private String rowObj;
	private JSONObject jsonObj;
	private JSON root;
	@Resource
	private IMenuService menuService;
	private JSONArray jsonArray;
	
	/**
	 * @Description:获取目录树
	 */
	@Action(value = "tree")
	public String showMenu(){
		List list=null;
		JSON data =null;
		if(SecurityUtils.getSubject().isAuthenticated()){
			list=menuService.showMenu(ShiroUtil.getCurrentUser().getId());
		}
		
		if(list!=null){
			data = JSONArray.fromObject(list);
		}else{
			String str="[{ \"id\":1,\"text\":\"没有权限\"}]";
			data = JSONArray.fromObject(str);
		}
       
		this.root = data;
		return "success";
		
	}

	@Action(value = "distributePermission")
	public String distributePermission(){
		List list=null;
		if(role!=0){
			list=menuService.distributePermission(role);
		}
		JSONObject data = new JSONObject();
		data.put("rows", list);
		jsonObj = data;
		return "jsonObj";
	}
	@Action(value = "editPermissions")
	public String editPermissions(){
		boolean permission = menuService.editPermissions(role, rowObj);
		JSONObject data = new JSONObject();
		if (permission) {
			data.put("success", true);
			data.put("message", "权限修改成功！");
		} else {
			data.put("success", false);
			data.put("message", "权限修改失败！");
		}
		this.jsonObj = data;
		return "jsonObj";
	}
	@Action(value = "showPer")
	public String showPermission() {
		JSONObject data = new JSONObject();
		List list=null;
		if(SecurityUtils.getSubject().isAuthenticated()){
		    list=menuService.showPermission(ShiroUtil.getCurrentUser().getId());
		}
		
		data.put("total", 0);
		data.put("rows", list);
		jsonObj = data;
		return "jsonObj";
	}
	
	
	public String getRowObj() {
		return rowObj;
	}

	public void setRowObj(String rowObj) {
		this.rowObj = rowObj;
	}
	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}

	public JSONObject getJsonObj() {
		return jsonObj;
	}

	public void setJsonObj(JSONObject jsonObj) {
		this.jsonObj = jsonObj;
	}

	public JSON getRoot() {
		return root;
	}

	public void setRoot(JSON root) {
		this.root = root;
	}

	public IMenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}

	public JSONArray getJsonArray() {
		return jsonArray;
	}

	public void setJsonArray(JSONArray jsonArray) {
		this.jsonArray = jsonArray;
	}

	
	
}
